package com.baidu.tcpThreadDemo1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class ServerConnectThread extends Thread{
    private Socket socket;

    public ServerConnectThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            //循环监控输入流是否有数据 有则输出
            String msg;
            while((msg = bufferedReader.readLine()) != null){
                System.out.println("线程:" + Thread.currentThread().getName()
                        + " 获取到客户端:" + socket.getRemoteSocketAddress() + "的消息: "
                        + msg);
            }
        } catch (Exception e) {
            System.out.println(socket.getRemoteSocketAddress() + "链接已断开");
        }
    }
}
